import movieProjectionView from '../views/movieProjection.art'
import movieProjectionModel from '../models/movieProjection'

class MovieProjection{

    constructor(){
        this.list = []
        this.offset = 0
        this.detailMovieList = []
        this.showDays = []
        this.Day
    }
    renderer(list,detailMovieList,showDays){
        let movieProjectionHtml = movieProjectionView({
            list,
            detailMovieList,
            showDays
        })
        $('main').html(movieProjectionHtml)
    }

    clickRender(ID){
        let that = this
        $('.timeline li').on('tap',async function(){ 
            $(this).addClass('chosen').siblings().removeClass('chosen')
            that.list = []
            let date = $(this).attr('data-date')
            let result = await movieProjectionModel.post({
                ID : ID,
                CityId : localStorage.getItem('cityId')||('1'),
                offset : that.offset,
                Day : date
            })
            let list = that.list = result.cinemas
            that.renderer(
                that.list,
                that.detailMovieList,
                that.showDays
            )
            $(`.timeline li[data-date= "${date}"]`).addClass('chosen').siblings().removeClass('chosen')
            if( $(`.timeline li[data-date= "${date}"]`).position().left>($('.timeline').width())/2 ){
                $('.timeline').scrollLeft( ($(`.timeline li[data-date= "${date}"]`).position().left) - ($('.timeline').width()/3) )
            }
            $('.left').on('tap',function(){
                window.history.back()
            })
            that.clickRender()
        })
    }

    async render(ID){
        let that =this
        $('footer').css('display','none')
        let detailMovie = await movieProjectionModel.getDetailMovie({
            movieId: ID
        })
        let detailMovieList = this.detailMovieList = [detailMovie.detailMovie]
        detailMovieList[0].img = detailMovieList[0].img.replace('w.h','148.208')
        detailMovieList[0].snum = (Math.round(detailMovieList[0].snum/1000))/10
        this.detailMovieList = detailMovieList

        if(detailMovieList[0].showst==3){
            this.Day = new Date().getFullYear()+`-`+(new Date().getMonth()+1) + `-` +new Date().getDate()
        }
        if(detailMovieList[0].showst==4){
            this.Day = detailMovieList[0].rt
        }
        let result = await movieProjectionModel.post({
            ID : ID,
            CityId : localStorage.getItem('cityId')||('1'),
            offset : this.offset,
            Day : this.Day
        })
        let list = this.list = result.cinemas
        let showDays = this.showDays = result.showDays.dates
        let date = new Date().getDay()
        for(let i in showDays){
            let days = ['日','一','二','三','四','五','六']
            this.showDays[i]['month'] = showDays[i].date.slice(-5,-3)
            this.showDays[i]['day'] = showDays[i].date.slice(-2)
            if(this.showDays[0]['day']==new Date().getDate()){
                if(i==0){
                    this.showDays[i]['days'] = '今天'
                    date += 1
                }
                if(i==1){
                    this.showDays[i]['days'] = '明天'
                    date += 1
                }
                if(i==2){
                    this.showDays[i]['days'] = '后天'
                    date += 1
                }
                if(i>=3){
                    this.showDays[i]['days'] = '周'+days[date]
                    date += 1
                }
                
                if(date>6){
                    date=0
                }
            }
            else{
                this.showDays[i]['days'] = '周'+days[date]
                date += 1
                if(date>6){
                    date=0
                }
            }
        }
        this.renderer(
            list,
            detailMovieList,
            this.showDays
        )
        $('main').scroll(async function(){
            if($('main').height()+$('main').scrollTop()==$('.cinema-container').height()+$('.movie-detail').height()){
                that.offset += 20
                let result = await movieProjectionModel.post({
                    ID : ID,
                    CityId : localStorage.getItem('cityId')||('1'),
                    offset : that.offset,
                    Day : that.Day
                })
                let list = result.cinemas
                that.list = [...that.list,...list]
                that.renderer(
                    that.list,
                    that.detailMovieList,
                    that.showDays
                )
                $('.left').on('tap',function(){
                    window.history.back()
                })
            }
        })
        this.clickRender(ID)

        $('.left').on('tap',function(){
            window.history.back()
        })

    }
}

export default new MovieProjection()